package com.ruoyi.project.modules.role.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.project.modules.business.domain.BusinessInfo;
import com.ruoyi.project.modules.businessUsage.domain.BusinessUsage;
import com.ruoyi.project.modules.usage.domain.Usage;
import com.ruoyi.project.modules.usage.domain.UsageParticipateRole;
import com.ruoyi.project.modules.usage.service.IUsageParticipateRoleService;
import com.ruoyi.project.modules.usage.service.IUsageService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.modules.role.domain.ParticipateRole;
import com.ruoyi.project.modules.role.service.IParticipateRoleService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 参与角色Controller
 *
 * @author smallrain
 * @date 2021-11-04
 */
@Controller
@RequestMapping("/role/role")
public class ParticipateRoleController extends BaseController {
    private String prefix = "role/role";

    @Autowired
    private IParticipateRoleService participateRoleService;

    @RequiresPermissions("role:role:view")
    @GetMapping()
    public String role() {
        return prefix + "/role";
    }

    /**
     * 查询参与角色列表
     */
    @RequiresPermissions("role:role:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ParticipateRole participateRole) {
        startPage();
        List<ParticipateRole> list = participateRoleService.selectParticipateRoleList(participateRole);
        return getDataTable(list);
    }

    /**
     * 导出参与角色列表
     */
    @RequiresPermissions("role:role:export")
    @Log(title = "参与角色", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ParticipateRole participateRole) {
        List<ParticipateRole> list = participateRoleService.selectParticipateRoleList(participateRole);
        ExcelUtil<ParticipateRole> util = new ExcelUtil<ParticipateRole>(ParticipateRole.class);
        return util.exportExcel(list, "参与角色数据");
    }

    /**
     * 新增参与角色
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存参与角色
     */
    @RequiresPermissions("role:role:add")
    @Log(title = "参与角色", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ParticipateRole participateRole) {
        return toAjax(participateRoleService.insertParticipateRole(participateRole));
    }

    /**
     * 修改参与角色
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        ParticipateRole participateRole = participateRoleService.selectParticipateRoleById(id);
        mmap.put("participateRole", participateRole);
        return prefix + "/edit";
    }

    /**
     * 修改保存参与角色
     */
    @RequiresPermissions("role:role:edit")
    @Log(title = "参与角色", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ParticipateRole participateRole) {
        return toAjax(participateRoleService.updateParticipateRole(participateRole));
    }

    @Autowired
    private IUsageParticipateRoleService usageParticipateRoleService;

    /**
     * 删除参与角色
     */
    @RequiresPermissions("role:role:remove")
    @Log(title = "参与角色", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(participateRoleService.deleteParticipateRoleByIds(ids));
    }

    @Autowired
    private ISysDictDataService dictDataService;

    @GetMapping("/detail/{id}")
    public String details(@PathVariable("id") Long id, ModelMap mmap) {
        ParticipateRole participateRole = this.participateRoleService.selectParticipateRoleById(id);
        participateRole.setRoleType(dictDataService.selectDictLabel("roleType", participateRole.getRoleType()));
        mmap.put("participateRole", participateRole);
        return prefix + "/details";
    }

    @Autowired
    private IUsageService usageService;

    @PostMapping("/usageList")
    @ResponseBody
    public AjaxResult usageList(@RequestParam Long businessId) {
        UsageParticipateRole usageParticipateRole = new UsageParticipateRole();
        List<UsageParticipateRole> usageParticipateRoles = this.usageParticipateRoleService.selectUsageParticipateRoleList(usageParticipateRole);
        if (CollectionUtil.isNotEmpty(usageParticipateRoles)) {
            Set<Long> usageIds = usageParticipateRoles.stream().map(UsageParticipateRole::getUsageId).collect(Collectors.toSet());
            Usage usage = new Usage();
            usage.setUsageIds(usageIds);
            List<Usage> usageList = this.usageService.selectUsageList(usage);
            return AjaxResult.success(usageList);
        }
        return AjaxResult.success(new ArrayList<Usage>());
    }
}
